
//app.js
var Promise = require('./utils/es6-promise.min.js').Promise;
const urls = require('./utils/url.js').urls;

App({
  onLaunch: function() {
    //调用API从本地缓存中获取数据 
    console.log(urls.login)
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.getUserInfo();
  },
  getUserInfo: function(cb) {
    var that = this

    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      login(that, cb);
    }
  },
  globalData: {
    userInfo: null,
    openid: null,
    user: null
  }
})
//调用登录接口
function login(that, cb) {
  // wx.navigateTo({ 
  //   url: 'pages/login/login',
  // })
  new Promise(function(resolve) {
    // // 显示加载中 
    // wx.showToast({
    //   title: "loading",
    //   icon: 'loading'
    // });
    wx.login({
      success: function(res) {
        resolve(res);
      }
    });
  }).then(function(xdata) {
    return new Promise(function(resolve, reject) {

      wx.request({
        url: urls.login,
        data: {
          code: xdata.code
        },
        success: function(res) {
          var data = res.data;

          if (data.errcode !== 500) {
            wx.setStorageSync("sessionId", data.sessionId);
            resolve(res);
          } else {
            reject(data);
          }
        }
      })
    });
  }).then(function(xdata) {
      return new Promise(function(resolve, reject) {
        wx.getUserInfo({
          success: function(res) {
            console.log(res);
            that.globalData.userInfo = res.userInfo
            resolve(res);
          },
          fail: function(res) {
            // 取消授权
            console.log(res)
            reject(res);
          }
        })
      });
    },
    function(xdata) {
      wx.showModal({
        title: '提示',
        content: xdata.errmsg,
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }).then(function(xdata) {
      return new Promise(function(resolve, reject) {
        wx.request({
          url: urls.info,
          data: {
            rawData: xdata.rawData,
            signature: xdata.signature,
            encryptedData: xdata.encryptedData,
            iv: xdata.iv
          },
          header: {
            'wxa-sessionid': wx.getStorageSync("sessionId")
          },
          success: function(res) {
            that.globalData.openid = res.data.openid;
            that.globalData.user = res.data;

            if (that.userInfoReadyCallback) {
              that.userInfoReadyCallback(res)
            }
            //检测是用户还是运维
            if (res.data.type == 0) {
              console.log("用户");
              //检测是否完善过信息
              if (res.data.room == undefined) {
                console.log("未认证");
                wx.reLaunch({
                  url: '/pages/user/user',
                })
              } else {
                console.log("已认证");
                wx.reLaunch({
                  url: '/pages/user/home/home',
                })
              }
            } else if (res.data.type == 1) {
              console.log("运维");
              wx.reLaunch({
                url: '/pages/operation/operation',
              })
            }else{
              console.log("去登录");
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }

          }
        })
      });
    },


    function(xdata) {
      // wx.hideToast();
      wx.redirectTo({
        url: '/pages/login/login',
      })

      // wx.showModal({
      //   title: '提示',
      //   content: "请确认授权读取您的用户昵称",
      //   success: function (res) {
      //     if (res.confirm) {

      //     }
      //   }
      // });
    }).then(function(xdata) {
    wx.hideToast();
    cb(that.globalData.userInfo);
  }, function(xdata) {
    wx.hideToast();
    wx.showModal({
      title: '提示',
      content: xdata.errmsg,
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    });
  });
}